import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../components/Login.vue"
import Home from "../components/Home.vue"
import Welcome from "../components/Welcome.vue"
import Users from "../components/User/User.vue"
import Rights from "../components/Rights/Rights.vue"
import Roles from"../components/Rights/Roles.vue"
import Cate from "../components/Goods/Cate.vue"
import Params from "../components/Goods/Params.vue"
import List from "../components/Goods/List.vue"
import Add from "../components/Goods/Add.vue"
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/Login'
  },
  {
    path:'/Login',
    component:Login
  },
  {
    path:'/Home',
    component:Home,
    redirect:'/welcome',
    children:[
      {path:'/welcome',component:Welcome},
      {path:'/users',component:Users},
      {path:'/rights',component:Rights},
      {path:'/roles',component:Roles},
      {path:'/categories',component:Cate},
      {path:'/params',component:Params},
      {path:'/list',component:List},
      {path:'/goods/add',component:Add}
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{
  //to 将要访问的路径
  //from 从哪个路径跳转而来
  //next 是一个函数表示放行
  if(to.path==='/Login') return next();
 const tokenStr= window.sessionStorage.getItem('token');
 if(!tokenStr) return next('/Login');
 next();
})

export default router
